package com.wuercloud.swagger;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Type;

/**
 * Created by Administrator on 2017/3/25.
 */
public class SwaggerFastJsonHttpMessageConverter4 extends FastJsonHttpMessageConverter4 {
    private ObjectMapper mapper = new ObjectMapper();

    @Override
    protected void writeInternal(Object obj, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        if (type == springfox.documentation.swagger.web.UiConfiguration.class) {
            HttpHeaders headers = outputMessage.getHeaders();
            ByteArrayOutputStream outnew = new ByteArrayOutputStream();
            mapper.writeValue(outnew, obj);
            outnew.flush();
            headers.setContentLength(outnew.size());
            OutputStream out = outputMessage.getBody();
            outnew.writeTo(out);
            outnew.close();
        } else {
            super.writeInternal(obj, type, outputMessage);
    }
    }

}